package com.sofar.gems.framework.controller;

import com.sofar.gems.framework.entity.SysUrl;
import com.sofar.gems.framework.entity.SysUser;
import com.sofar.gems.framework.service.SysUserService;
import com.sofar.gems.framework.vo.R;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: 用户控制器
 * @company: yaorange
 * @author: 啊啊啊林
 * @version: 1.0
 * @create: 2021-09-21 20:07:58
 */
@RestController
@RequestMapping("/user")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping
    public R<?> selectAll() {
        List<SysUser> all = sysUserService.selectAll();
        return R.success(all);
    }

    //登录验证
    @GetMapping("/login/{username}/{password}")
    public R<?> logins(@PathVariable String username,@PathVariable String password) {
        SysUser sysUser=new SysUser();
        sysUser.setUsername(username);
        sysUser.setPassword(password);
        SysUser sysUser1 = sysUserService.selectLogin(sysUser);
        if (sysUser1 == null) {
            return R.error("failue");
        }
        return R.success("success");
    }
    /**
    * @description:创建用户
    * @param sysUser
    * @return com.sofar.gems.framework.vo.R<?>
    * @exception
    */
    @PostMapping
    public R<?> insert(@RequestBody SysUser sysUser) {
        int i = sysUserService.insertSelective(sysUser);
        if (i > 0) {
            return R.success("success");
        } else {
            return R.error("插入失败");
        }
    }
/**
* @description:主键删除
* @param id
* @return com.sofar.gems.framework.vo.R<?>
* @exception
*/
    @DeleteMapping("/{id}")
    public R<?> delete(@PathVariable Integer id ) {
        int i = sysUserService.deleteByPrimaryKey(id);
        if (i > 0) {
            return R.success("success");
        } else {
            return R.error("删除失败");
        }
    }
/**
* @description: 非全参修改
* @param sysUser
* @return com.sofar.gems.framework.vo.R<?>
* @exception
*/
    @PutMapping
    public R<?> update(@RequestBody SysUser sysUser) {
        int i = sysUserService.updateByPrimaryKeySelective(sysUser);
        if (i > 0) {
            return R.success("修改成功");
        } else {
            return R.error("修改失败");
        }
    }
}
